package xyz.duolaf.demo01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCBasc {
    public static void main(String[] args) throws Exception {
        String driver ="com.mysql.jdbc.Driver";//数据库驱动信息
        String url="jdbc:mysql://localhost:3306/test?useSSL=true&useUnicode=true&characterEncoding=utf8";//数据库url
        String username="root";//数据库用户
        String password="root";//数据库密码

        //1. 加载数据库驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2. 连接数据库,获取connection
        Connection connection = DriverManager.getConnection(url, username, password);

        //3. 创建执行器
        Statement statement = connection.createStatement();

        //4.执行sql
        String sql="select * from user";
        ResultSet resultSet = statement.executeQuery(sql);

        //5. 处理结果
        while (resultSet.next()){
            System.out.print(resultSet.getObject("id")+"\t");
            System.out.print(resultSet.getObject("username")+"\t");
            System.out.print(resultSet.getObject("password")+"\t");
            System.out.print(resultSet.getObject("age")+"\t");
            System.out.println();
        }

        //6. 释放连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}
